package com.onpositive.notes.android.ui.elements;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;

import com.onpositive.businessdroids.model.IField;
import com.onpositive.businessdroids.model.groups.IGroupingCalculator;
import com.onpositive.businessdroids.model.groups.SimpleFieldGroupingCalculator;
import com.onpositive.businessdroids.model.impl.AbstractCollectionBasedModel;
import com.onpositive.businessdroids.model.impl.AbstractComputableField;
import com.onpositive.businessdroids.model.impl.Column;
import com.onpositive.businessdroids.ui.IOpenListener;
import com.onpositive.businessdroids.ui.dataview.DataViewFactory;
import com.onpositive.businessdroids.ui.dataview.Group;
import com.onpositive.businessdroids.ui.dataview.StructuredDataView;
import com.onpositive.collections.CollectionsContainer.MemberWrapper;
import com.onpositive.collections.Delta;
import com.onpositive.collections.ICollectionMember;
import com.onpositive.collections.IObjectCollection;
import com.onpositive.notes.android.main.CollectionHistoryActivity;
import com.onpositive.notes.android.main.MemberHistoryActivity;
import com.onpositive.notes.model.INote;
import com.onpositive.notes.services.BasicCollectionRegistry;
import com.onpositive.notes.services.BasicNoteRegistry;

public class AllThingsUI {

	private StructuredDataView sdv;

	public View createView( final Context context ){
		
		initDataView( context );		
		LinearLayout lt = new LinearLayout(context);
		lt.addView(sdv, new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));		
		return lt ;
	}
	
	public void initDataView( final Context context )
	{
		IField nameField = new AbstractComputableField() {

			@Override
			public Object getPropertyValue(Object object) {

				if (object == null)
					return "NULL";

				if (object instanceof ICollectionMember){
					ICollectionMember<?> member = (ICollectionMember<?>) object ;
					return member.getName() ;
				}
				else if( object instanceof IObjectCollection )
				{
					IObjectCollection collection = (IObjectCollection) object ;
					return "Collection itself: "+collection.getName() ;
				}
				return object.toString();
			}
		};
		
		IField valueField = new AbstractComputableField() {

			@Override
			public Object getPropertyValue(Object object) {

				if (object == null)
					return "NULL";

				if (object instanceof ICollectionMember){
					ICollectionMember<?> member = (ICollectionMember<?>) object ;
					return member.getCurrentValue() ;
				}

				return "" ;
			}
		};
		Column nColumn = new Column(nameField) ;
		nColumn.setId("name") ;
		nColumn.setCaption(true) ;
		Column vColumn = new Column(valueField) ;
		vColumn.setId("value") ;

		IField[] fields = new IField[]{ nColumn, vColumn } ;//nameField, valueField  } ;
		
		List<Object> allMembers = getAllMmbers() ;
		
		sdv = DataViewFactory.createTable(
				context, allMembers, null, null, fields);
		
		sdv.setActionBarVisible(true) ;
		sdv.setHeaderVisible(true) ;
		sdv.setFooterVisible(true) ;

		sdv.setOpenListener(new IOpenListener() {

			@Override
			public void openElement(View parent, View elementView, Object element, int pos)
			{
				if( !(context instanceof Activity) )
					return ;
				
				Activity activity = (Activity) context ;
				if( element instanceof IObjectCollection ){
					IObjectCollection collection = (IObjectCollection) element;
	
					Intent intent = new Intent(context, CollectionHistoryActivity.class);
					intent.putExtra(UIConstants.EXTRA_KEY_COLLECTION, collection.getId());					
					activity.startActivityForResult(intent,0 ) ;
				}
				else if( element instanceof ICollectionMember ){
					ICollectionMember<?> member = (ICollectionMember<?>) element;
					if( member instanceof MemberWrapper ){
						MemberWrapper wrapper = (MemberWrapper) member ;
						member = wrapper.getMember() ;
					}
	
					Intent intent = new Intent(context, MemberHistoryActivity.class);
					intent.putExtra(UIConstants.EXTRA_KEY_COLLECTION, member.getCollectionId());
					intent.putExtra(UIConstants.EXTRA_KEY_MEMBER, member.getId());
					activity.startActivityForResult(intent,0 ) ;
				} 
			}
		});
		
		IField collectionField = new AbstractComputableField(){

			@Override
			public Object getPropertyValue(Object object) {
				
				if (object == null)
					return "NULL";

				if (object instanceof ICollectionMember){
					ICollectionMember<?> member = (ICollectionMember<?>) object ;
					return member.getCollection().getName() ;
				}
				else if( object instanceof IObjectCollection )
				{
					IObjectCollection collection = (IObjectCollection) object ;
					return collection.getName() ;
				}
				return object.toString();
			}
		} ;
		
		Column column = new Column( collectionField ) ;
		column.setId( "collection-id" ) ;
		IGroupingCalculator gCalc = new SimpleFieldGroupingCalculator( column ) ;
		sdv.getTableModel().setCurrentGrouping( gCalc ) ;		
	}
	
	private List<Object> getAllMmbers() {
		
		ArrayList<Object> result = new ArrayList<Object>() ;
		List<IObjectCollection> allCollections = BasicCollectionRegistry.getInstance().getAllCollections();
		for( IObjectCollection collection : allCollections ){
			result.add(collection) ;
			result.addAll(collection.getAllMembers()) ;
		}
		
		return result ;
	}
	
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		
		if( sdv == null )
			return ;
			
			sdv.getTableModel().fireModelChanged();
	}	

	
}
